<template>
    <el-dialog :title="tit===0?'添加':'编辑'" :visible.sync="centerDialogVisible" width="30%" center
        :before-close="handleClose">
        <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
            <el-form-item label="课程名称" prop="books">
                <el-input v-model=" ruleForm.books"></el-input>
            </el-form-item>
            <el-form-item label="教师姓名" prop="teacher">
                <el-input v-model=" ruleForm.teacher"></el-input>
            </el-form-item>
            <el-form-item label="课程地点" prop="value">
                <el-cascader v-model="ruleForm.value" :options="options"></el-cascader>
            </el-form-item>
            <el-form-item label="课程时间" required>
                <el-col :span="11">
                    <el-date-picker type="date" placeholder="选择日期" v-model="ruleForm.date1" style="width: 100%;">
                    </el-date-picker>
                </el-col>
                <el-col class="line" :span="2">-</el-col>
                <el-col :span="11">
                    <el-time-picker placeholder="选择时间" v-model="ruleForm.date2" style="width: 100%;"></el-time-picker>
                </el-col>
            </el-form-item>
            <el-form-item label="听课人数" prop="numbers">
                <el-input v-model=" ruleForm.numbers"></el-input>
            </el-form-item>
            <el-form-item label="课程价格" prop="price">
                <el-input v-model=" ruleForm.price"></el-input>
            </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
            <el-button @click="resetForm('ruleForm')">取 消</el-button>
            <el-button type="primary" @click="submitForm('ruleForm')">{{tit===0?'添加':'确认'}}</el-button>
        </span>
    </el-dialog>
</template>

<script>
    import area from "../../../assets/area.json";
    import {
        chenqaddlist,
        chenqchangelist
    } from "../../../api/interface.js";
    export default {
        props: ['tit', 'row'],
        data() {
            return {
                centerDialogVisible: true,
                options: area,
                ruleForm: {
                    books: "",
                    teacher: "",
                    numbers: "", //人数
                    price: "",
                    date1: '',
                    date2: '',
                    value: [],
                },
                rules: {
                    books: [{
                            required: true,
                            message: '请输入课程名称',
                            trigger: 'blur'
                        },
                        {
                            min: 2,
                            max: 10,
                            message: '长度在 2 到 10 个字符',
                            trigger: 'blur'
                        }
                    ],
                    teacher: [{
                            required: true,
                            message: '请输入教师姓名',
                            trigger: 'blur'
                        },
                        {
                            min: 2,
                            max: 8,
                            message: '长度在 2 到 8 个字符',
                            trigger: 'blur'
                        }
                    ],
                    numbers: [{
                        required: true,
                        message: '请输入听课人数',
                        trigger: 'blur'
                    }],
                    price: [{
                        required: true,
                        message: '请输入课程价格',
                        trigger: 'blur'
                    }],
                    date1: [{
                        type: 'date',
                        required: true,
                        message: '请选择日期',
                        trigger: 'change'
                    }],
                    date2: [{
                        type: 'date',
                        required: true,
                        message: '请选择时间',
                        trigger: 'change'
                    }],
                    value: [{
                        required: true,
                        message: '请输入地点',
                        trigger: 'change'
                    }],
                }
            }
        },
        mounted() {
            this.fom();
        },

        methods: {
            fom() {
                this.ruleForm.books = this.row.books;
                this.ruleForm.teacher = this.row.teacher;
                this.ruleForm.numbers = this.row.numbers; //人数
                this.ruleForm.price = this.row.price;
                this.ruleForm.date1 = '';
                this.ruleForm.date2 = '';
                this.ruleForm.value = [];
            },
            getAddressData(address, area) {
                //把address地址格式转换成贵州省 安顺市 西秀区
                return address.map(item => {
                    for (var val of area) {
                        if (val.value == item) {
                            area = val.children
                            return val.label
                        }
                    }
                })
            },
            submitForm(formName) {
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        var moment = require('moment');
                        var da = new Date(this.ruleForm.date1);
                        // console.log(moment(da).format('YYYY-MM-DD h:mm:ss'));
                        var {
                            books,
                            teacher,
                            numbers,
                            price,
                            date1,
                            date2,
                            value
                        } = this.ruleForm;
                        var li = this.getAddressData(value, area)
                        var obje = {
                            books,
                            teacher,
                            numbers,
                            price,
                            time: moment(da).format('YYYY-MM-DD h:mm:ss'),
                            address: li.join(" ")
                        }
                        if (this.tit == 0) {
                            chenqaddlist(obje).then(res => {
                                if (res.code == 200) {
                                    this.$emit("getnum");
                                    this.$emit("changeshow");
                                }
                            })
                        } else {
                            obje.id = this.row.id;
                            chenqchangelist(obje).then(res => {

                                if (res.code == 200) {
                                    this.$emit("getnum");
                                    this.$emit("changeshow");
                                }
                            })
                        }
                    } else {
                        return false;
                    }
                });
            },
            resetForm(formName) {
                this.$refs[formName].resetFields();
            },
            handleClose() {
                this.centerDialogVisible = true;
                this.$emit("changeshow");
            }
        }
    }
</script>

<style scoped>

</style>